package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Supplier;

@Service
public class ItemServiceImpl implements ItemService {

    //远程调用
    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ListFeignClient listFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;




    //=======================================================
    //获取sku详情信息
    @Override
    public Map<String, Object> getBySkuId(Long skuId) {
        Map<String,Object> result = new HashMap<>();

        //使用异步编排
        //1.A 创建异步对象,有返回值
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(new Supplier<SkuInfo>() {
            @Override
            public SkuInfo get() {
                //通过skuId查询skuInfo
                SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
                //放入到result中
                result.put("skuInfo", skuInfo);
                return skuInfo;
            }
        },threadPoolExecutor);
        //2.B->A 通过sku表的三级分类id获取一级分类名称、二级分类名称和三级分类名称
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView", categoryView);
        },threadPoolExecutor);
        //3.AA 获取价格信息
        CompletableFuture<Void> skuPriceCompletableFuture = CompletableFuture.runAsync(() -> {
            //获取价格信息
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            result.put("price", skuPrice);
        },threadPoolExecutor);
        //4.C->A 销售属性+销售属性值回显并锁定
        CompletableFuture<Void> spuSaleAttrCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCherBySku(skuId, skuInfo.getSpuId());
            result.put("spuSaleAttrList", spuSaleAttrList);
        },threadPoolExecutor);
        //5.D->A 在后台需要查询出spuId下,对应多少sku
        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            //保存json字符串
            String valueSkuJson = JSON.toJSONString(skuValueIdsMap);
            result.put("valuesSkuJson", valueSkuJson);
        },threadPoolExecutor);

        /**
         * 第九天:调用热度排名
         */
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(()->{
            listFeignClient.increHotScore(skuId);
        },threadPoolExecutor);

        //多任务组合
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                categoryViewCompletableFuture,
                skuPriceCompletableFuture,
                spuSaleAttrCompletableFuture,
                valuesSkuJsonCompletableFuture,
                completableFuture
                ).join();





  /*      //通过skuId查询skuInfo
        SkuInfo skuInfo = productFeignClient.getAttrValueList(skuId);
        //销售属性+销售属性值回显并锁定
        List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCherBySku(skuId, skuInfo.getSpuId());
        //******根据销售属性值进行切换商品
        //在后台需要查询出spuId下,对应多少sku
        Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
        //获取价格信息
        BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
        //通过sku表的三级分类id获取一级分类名称、二级分类名称和三级分类名称
        BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());

        //保存数据
        result.put("categoryView",categoryView);
        result.put("price",skuPrice);
        result.put("skuInfo", skuInfo);
        //保存json字符串
        String valueSkuJson = JSON.toJSONString(skuValueIdsMap);
        result.put("valuesSkuJson",valueSkuJson);
        result.put("spuSaleAttrList",spuSaleAttrList);
   */
        return result;
    }
}
